package com.example.base.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.base.config.PageData;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.system.domain.SysLog;
import com.example.base.system.service.SysLogService;
import com.example.base.utils.DateUtils;
import com.example.base.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.base.limit.IPRateLimit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.Serializable;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X.I.O
 * @since 2024-02-24
 */

@RestController
@Api(tags = {"的服务类"})
@RequestMapping("/sys-log")
public class SysLogController  {

        @Autowired
        private SysLogService sysLogService;





        @PostMapping("/save")
        @IPRateLimit(name = "SysLog-save",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的新增接口",notes = "的新增接口")
        public JSONObject save(@RequestBody SysLog sysLog) throws GlobalExcetion {
                 sysLogService.saveSysLog(sysLog);
                 return Result.successInfo("保存成功");
        }


        @PostMapping("/update")
        @IPRateLimit(name = "SysLog-update",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的更新接口",notes = "的更新接口")
        public JSONObject update(@RequestBody SysLog sysLog) throws GlobalExcetion{
                sysLogService.updateSysLogById(sysLog);
                return Result.successInfo("更新成功");
        }



        @DeleteMapping("/delbyid")
        @IPRateLimit(name = "SysLog-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的删除接口",notes = "的删除接口")
        public JSONObject delete(@RequestParam("id") Serializable id) throws GlobalExcetion{
                boolean flag = sysLogService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysLog-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的批量删除接口",notes = "的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
                boolean flag =  sysLogService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysLog-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取全部接口",notes = "的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
                List<SysLog> list= sysLogService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/getbyid")
        @IPRateLimit(name = "SysLog-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取一个接口",notes = "的获取一个接口")
        public JSONObject findOne(@RequestParam("id") Serializable id) throws GlobalExcetion {
                SysLog data= sysLogService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysLog-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的分页接口",notes = "的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
                IPage<SysLog> data=sysLogService.page(page, pageSize);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }

        @GetMapping("getlogtotal")
        public JSONObject getLogTotal(@RequestParam(value = "startDate",required = false)String startDate,
                                      @RequestParam(value = "endDate",required = false)String endDate){
                List<Map<String,Object>> mapList=sysLogService.getPast15DaysTotal();
                Map<String,Integer> map=new HashMap<>();
                mapList.forEach((e->{
                        Date date=(Date) e.get("date");
                        String dateStr=new SimpleDateFormat("yyyy-MM-dd").format(date);
                        System.out.println(dateStr);
                        Integer total=Integer.parseInt(String.valueOf(e.get("total")));
                        map.put(dateStr,total);
                }));
                List<String> strings=DateUtils.getPast15Days();
                List<String> dateList=new ArrayList<>();
                List<Integer> totalList=new ArrayList<>();
                for (int i = 0; i < strings.size() ; i++) {
                        String date=strings.get(i);
                        Integer total=map.get(date);
                        dateList.add(date);
                        total = total == null ? 0 : total;
                        totalList.add(total);
                }
                JSONObject obj=new JSONObject();
                obj.put("dateList",dateList);
                obj.put("totalList",totalList);
                return Result.successInfo(obj);
        }
}

